package com.fzj.example.io;

import java.io.*;
import java.util.Properties;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

/**
 * ClassName: Example
 * date: 2019/11/11 14:21
 * Description: TODO
 *
 * @author fanzj@bonree.com
 * @since JDK 1.8
 */
public class Example {
    public static void main(String[] args) throws IOException {
        /**********************RandomAccessFile***************************/
        RandomAccessFile randomAccessFile=new RandomAccessFile("D:\\self_domain\\server\\language\\JAVA\\java培训\\java基础\\IO\\random.txt","rw");
/*        randomAccessFile.read();
        randomAccessFile.readByte();
        randomAccessFile.readChar();
        randomAccessFile.write();
        randomAccessFile.writeByte();
        randomAccessFile.writeChar();*/
        //
        for (int i = 0; i < 10; i++) {
            randomAccessFile.writeDouble(1.44*i);
        }
        randomAccessFile.close();
        //
        randomAccessFile=new RandomAccessFile("D:\\self_domain\\server\\language\\JAVA\\java培训\\java基础\\IO\\random.txt","rw");
        //直接将文件指针移到第5个double数据，替换
        randomAccessFile.seek(5*8);
        randomAccessFile.writeDouble(22l);
        randomAccessFile.close();
        randomAccessFile=new RandomAccessFile("D:\\self_domain\\server\\language\\JAVA\\java培训\\java基础\\IO\\random.txt","rw");
        for (int i = 0; i < 10; i++) {
            System.out.println("Value"+i+"="+randomAccessFile.readDouble());
            
        }
        /**********************Properties ***************************/
        Properties properties=new Properties();
        //
        properties.load(Example.class.getClassLoader().getResourceAsStream("data.properties"));
        System.out.println(properties.get("test"));
        /**********************ZipOutputStream ***************************/
        FileInputStream fileInputStream=new FileInputStream("D:\\self_domain\\server\\language\\JAVA\\java培训\\java基础\\IO\\zip.txt");
        BufferedInputStream bis=new BufferedInputStream(fileInputStream);
        ZipOutputStream zipOutputStream=new ZipOutputStream(new FileOutputStream("D:\\self_domain\\server\\language\\JAVA\\java培训\\java基础\\IO\\zip2.zip"));
        BufferedOutputStream bos=new BufferedOutputStream(zipOutputStream);
        zipOutputStream.putNextEntry(new ZipEntry("zip.txt"));
        zipOutputStream.setComment("test zip output");
        byte[] b=new byte[1024];
        int len;
        while ((len=bis.read(b))!=-1){
            bos.write(b,0,len);
        }
        bos.close();
        bis.close();
        /**********************ZipInputStream ***************************/
        File fileZip=new File("D:\\self_domain\\server\\language\\JAVA\\java培训\\java基础\\IO\\zip2.zip");
        ZipFile zipFile=new ZipFile(fileZip);
        ZipInputStream zipInputStream=new ZipInputStream(new FileInputStream(fileZip));
        ZipEntry zipEntry=null;
        while ((zipEntry=zipInputStream.getNextEntry())!=null){
            System.out.println("decompress file"+zipEntry.getComment()+":"+zipEntry.getName());
            File outFile=new File("D:\\self_domain\\server\\language\\JAVA\\java培训\\java基础\\IO\\zip2\\"+zipEntry.getName());
            //判断文件上级目录存在性是否存在
            if(!outFile.getParentFile().exists()){
                outFile.getParentFile().mkdir();
            }
            if(!outFile.exists()){
                outFile.createNewFile();
            }
            BufferedInputStream bufferedInputStream=new BufferedInputStream(zipFile.getInputStream(zipEntry));
            BufferedOutputStream bufferedOutputStream=new BufferedOutputStream(new FileOutputStream(outFile));
            byte[] bytes=new byte[1024];
            int len2=0;
            while ((len2=bufferedInputStream.read(bytes))!=-1){
                bufferedOutputStream.write(bytes,0,len2);
            }
            bufferedOutputStream.close();
            bufferedInputStream.close();
        }
        //压缩文件流
        zipInputStream.close();
    }
}
